package com.stack.knowyouservice.service.impl;

import com.stack.knowyoubase.anotional.TimeOut;
import com.stack.knowyoubean.bean.KnowLedge;
import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoudao.dao.KnowLedgeDao;
import com.stack.knowyouservice.service.KnowLedgeService;
import com.stack.knowyoubase.util.PageUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 知识点服务层
 *
 * @author stack
 */
@Service
@Scope("singleton")
public class KnowLedgeServiceImpl implements KnowLedgeService {
    @Resource
    private KnowLedgeDao knowLedgeDao;
    @Resource
    private PageUtil pageUtil;


    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public HashMap<String, Object> knowLedgeSearch(Integer sid, String kname, String kclass, String kchap, Integer page, Integer limit, Authentication authentication) {
        //课程号来源于session，课程号是安全的
        HashMap<String, Integer> pageMap = pageUtil.pageCreate(page, limit);
        //模糊查询参数补全
        if (!StringUtils.isEmpty(kname)) {
            kname = "%" + kname + "%";
        }
        if (!StringUtils.isEmpty(kclass)) {
            kclass = "%" + kclass + "%";
        }
        if (!StringUtils.isEmpty(kchap)) {
            kchap = "%" + kchap + "%";
            ;
        }
        Integer knowLedgeCount = knowLedgeDao.findKnowLedgeCount(sid, kname, kclass, kchap, pageMap.get("page"), pageMap.get("limit"));
        HashMap<String, Object> knowLedgeMap = new HashMap<>(2);
        if (knowLedgeCount == 0) {
            knowLedgeMap.put("count", 1);
            knowLedgeMap.put("knowLedgeList", Arrays.asList(new KnowLedge()));
        } else {
            //验证角色
            String role = "";
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            for (GrantedAuthority authority : authorities) {
                role = authority.getAuthority();
            }
            if (GlobalConst.TEACHER_ROLE.equals(role)) {
                List<KnowLedge> knowLedge = knowLedgeDao.findKnowLedgeForTc(sid);
                knowLedgeMap.put("count", knowLedgeCount);
                knowLedgeMap.put("knowLedgeList", knowLedge);
            } else {
                List<KnowLedge> knowLedge = knowLedgeDao.findKnowLedge(sid, kname, kclass, kchap, pageMap.get("page"), pageMap.get("limit"));
                knowLedgeMap.put("count", knowLedgeCount);
                knowLedgeMap.put("knowLedgeList", knowLedge);
            }
        }
        return knowLedgeMap;
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean knowLedgeAdd(KnowLedge knowLedge) {
        int i = knowLedgeDao.insertKnowLedge(knowLedge);
        return i == 1;
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean knowLedgeUpdate(KnowLedge knowLedge, Integer sid) {
        //判断该课程是否持有此知识点
        List<Integer> list = knowLedgeDao.knowLedgeBySid(sid);
        boolean contains = list.contains(knowLedge.getKid());
        if (!contains) {
            return false;
        } else {
            int i = knowLedgeDao.updateKnowLedge(knowLedge);
            return i == 1;
        }
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean knowLedgeDelete(Integer kid, Integer sid) {
        //判断该课程是否持有此知识点
        List<Integer> list = knowLedgeDao.knowLedgeBySid(sid);
        boolean contains = list.contains(kid);
        if (!contains) {
            return false;
        } else {
            int i = knowLedgeDao.deleteKnowLedge(kid);
            return i == 1;
        }
    }
}
